﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        clsUser objUser = new clsUser();
        DataSet dt = objUser.UserConsult(txtUserName.Text, txtPassword.Text);

        if (dt.Tables[0].Rows.Count > 0 && !txtPassword.Text.Contains("||"))
        {
            //User Data
            string idUser = dt.Tables[0].Rows[0].ItemArray[0] + "";
            string idPNatural = dt.Tables[0].Rows[0].ItemArray[1] + "";
            string idRole = dt.Tables[0].Rows[0].ItemArray[2] + "";

            //Role of user data
            clsRole objRole = new clsRole();
            DataSet dtRole = objRole.ConsultID(idRole);
            string flgInsert = dtRole.Tables[0].Rows[0].ItemArray[1] + "";
            string flgUpdate = dtRole.Tables[0].Rows[0].ItemArray[2] + "";
            string flgDelete = dtRole.Tables[0].Rows[0].ItemArray[3] + "";
            //Send the cookie from our state
            #region STATE
                HttpCookie objCookState = new HttpCookie("state", "true");
                Response.Cookies.Add(objCookState); 
            #endregion
            //Send cookies to the flag
            #region COOKIES_FLAG
                HttpCookie objCookInsert = new HttpCookie("flgInsert", flgInsert);
                HttpCookie objCookUpdate = new HttpCookie("flgUpdate", flgUpdate);
                HttpCookie objCookDelete = new HttpCookie("flgDelete", flgDelete);
                Response.Cookies.Add(objCookInsert);
                Response.Cookies.Add(objCookUpdate);
                Response.Cookies.Add(objCookDelete);
            #endregion            
            //Data's Role
            clsProfile objProfile = new clsProfile();
            DataSet dtProfile = objProfile.ConsultRoleID(idRole);
            //built the program chain identifiers
            string programName = "";
            if (dtProfile.Tables[0].Rows.Count > 0)
                programName = dtProfile.Tables[0].Rows[0].ItemArray[2] + "";
            
            for (int i = 1; i < dtProfile.Tables[0].Rows.Count; i++)
            {
                programName = programName + "," + dtProfile.Tables[0].Rows[i].ItemArray[2];
            
            }
            HttpCookie objCookProgramName = new HttpCookie("programName", programName);
            Response.Cookies.Add(objCookProgramName);
            Response.Redirect("index.aspx");            
        }
        else
        {
            lblMessage.Text = "Incorrect information, try again.";
        }
    }
}
